package com.glamour.hlcity.common.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * 资源下载
 * @author zhaoxing.liuzx
 *
 */
public class DownLoadUtil {
	
	/**
	 * 保存到本地
	 * @param downUrl
	 * @param filePath
	 * @return
	 */
	public static boolean saveHtml(String downUrl, String filePath) {//将downUrl的数据保存到filePath上
		final int BUFFER_SIZE = 1024;
		HttpURLConnection connect = null;
		BufferedInputStream in = null;
		FileOutputStream file = null;
		byte[] buf = new byte[BUFFER_SIZE];
		int size = 0;
		try {
			URL url = new URL(downUrl);
			
			connect = (HttpURLConnection) url.openConnection();
			// connect.connect();
			
			connect.setConnectTimeout(100);
			
			in = new BufferedInputStream(connect.getInputStream());
			file = new FileOutputStream(filePath);
			while ((size = in.read(buf)) != -1) {
				file.write(buf, 0, size);
			}
			file.close();
			in.close();
			connect.disconnect();
			 
		} catch (MalformedURLException e) {
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		} 
		return true;
	}
	
	/**
	 * 新建目录
	 * @param dir
	 * @return
	 */
	public static boolean mkdir(String dir){
		File file=new File(dir);
		if(!file.exists()){
			file.mkdir();
		}
		return true;
	}
	
	

}
